package com.android.lovingpet.adopt.host.behost.view;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.android.lovingpet.R;
import com.android.lovingpet.adopt.host.behost.BeHostPresenter;
import com.android.lovingpet.adopt.host.behost.IBeHostContract;
import com.android.lovingpet.adopt.host.behost.PetExp;
import com.android.lovingpet.base.TakePhotoFragment;
import com.android.lovingpet.util.MyPhotoWindow;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.jph.takephoto.model.TResult;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ikata on 2018/4/27.
 */

public class FamilyInfoFragment extends TakePhotoFragment<IBeHostContract.View, BeHostPresenter> implements IBeHostContract.View, View.OnClickListener {


    @BindView(R.id.iv_photo)
    ImageView ivPhoto;
    @BindView(R.id.bt_commit)
    Button btCommit;

    Unbinder unbinder;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_description)
    EditText etDescription;
    @BindView(R.id.et_attention)
    EditText etAttention;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_service)
    EditText etService;

    String name;
    String address;
    String phone;
    String description;
    String service;
    String attention;

    private List<PetExp> petExpList;

    File headFile;

    @Override
    protected int setContentView() {
        return R.layout.fragment_familyinfo;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        setTitle(view, "家庭资料");
    }

    @Override
    public BeHostPresenter createPresenter() {
        return new BeHostPresenter();
    }

    @Override
    public IBeHostContract.View createView() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.bt_commit, R.id.iv_photo})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_commit:
                name = etName.getText().toString();
                address = etAddress.getText().toString();
                phone = etPhone.getText().toString();
                description = etDescription.getText().toString();
                service = etService.getText().toString();
                attention = etAttention.getText().toString();
                if (petExpList == null) {
                    Toast.makeText(getActivity(), "养宠经验未填写!", Toast.LENGTH_LONG).show();
                    break;
                }
                if (headFile != null && !TextUtils.isEmpty(name) && !TextUtils.isEmpty(address) && !TextUtils.isEmpty(phone) && !TextUtils.isEmpty(description) && !TextUtils.isEmpty(service) && !TextUtils.isEmpty(attention)) {
                    //先获取经纬度
                    getPresenter().getAddress();
                } else
                    Toast.makeText(getActivity(), "请将资料填写完整!", Toast.LENGTH_LONG).show();
                break;
            case R.id.iv_photo:
                new MyPhotoWindow(takePhoto, getView()).create();

                break;
            default:
                break;
        }
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        headFile = new File(result.getImage().getOriginalPath());
        Glide.with(this)
                .load(headFile)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(ivPhoto);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        Toast.makeText(getActivity(), "Error:" + msg, Toast.LENGTH_SHORT).show();
    }


    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void getPetExp(List<PetExp> list) {
        petExpList = list;
    }

    @Override
    public void next(@Nullable Bundle bundle) {
        if (bundle == null) {
            getActivity().finish();
            return;
        }
        AMapLocation aMapLocation = bundle.getParcelable("result");
        double longitude = aMapLocation.getLongitude();
        double latitude = aMapLocation.getLatitude();
        String petExp = new Gson().toJson(petExpList);
        getPresenter().beHost(name, address, phone, description, service, attention, petExp,longitude,latitude, headFile);
    }

}